import {useTokenUtilgenerateRandomNumber} from "@/utils/tokenUtils"
import type {AimodelParams, Image, Img2imgParams} from "~/composables/drawhooks";
import type {ComfyUIProperties, WorkFlow} from "~/utils/comfyuiApi";


export const useStableDiffusionStore = defineStore({
    id: "stableDiffusion",
    state: () => ({
        ckpt_id: 0,  //大模型id
        isFixSeed: false,
        excutingApi: "",
        status: {
            progress: 0,
            server: '',
            queue: 0,
            time_remained:0
        },
        //API
        txt2imgParams: {
            positive: "一个女孩",
            negative: "丑陋的",
            seed: 0,
            width: 512,
            height: 768,
            ckpt_name: "",
            upscale_by: 1,
        } as ComfyUIProperties,
        img2imgParams: {
            positive: 'a beautiful girl',
            negative: '',
            ckpt_name: '',
            denoise: 0.3,
        } as ComfyUIProperties,
        aimodelParams: {
            image_path_origin: "",
            image_path_mask: "",
            segmentpart: "",
            positive: "纯色背景",
            negative: "丑陋的",
            ckpt_name: "",
            denoise: 0.8
        } as ComfyUIProperties,
        aistudioParams: {} as ComfyUIProperties,
        ouputimgList: [] as Array<Image>,
        //图生图
        imageParam_img2img: [] as Array<Image>,
        //AI写真
        imageParam_aistudio: [] as Array<Image>,
        imageParam2_aistudio: [] as Array<Image>,
        //AI模特
        imageParam_aimodel: [] as Array<Image>,
        outputImage_Mask: {} as Image,

        //模型清单
        modelList: [],
        currentModel: "",
        isGernateDisable: false,
        btnStatus: "AI绘图中",//按钮显示的消息状态
        node: "",//节点名称
        useLocalComfyUI: false,
        localComfyUIUrl: 'http://127.0.0.1:8188',
        sd3_key: '',
        useOss: true,//启用OSS
        useCustomizeOss: false, //启用自定义OSS
        ossConfig: {
            ossRegion: "",
            ossKey: "",
            ossSecret: "",
            ossBucket: ""
        },
        workflows: [] as WorkFlow[],
    }),

    persist: [{
        // 可以指定任何 extends Storage 的实例，默认是 sessionStorage
        storage: persistedState.localStorage,
        // state 中的字段名，按组打包储存
        paths: ["localVitsUrl", "workflows", "ouputimgList", "txt2imgParams", "img2imgParams", "aistudioParams", "aimodelParams", 'localComfyUIUrl', 'useLocalComfyUI', 'sd3_key', 'useOss', 'useCustomizeOss', 'ossConfig']
    }
    ],
    getters: {
        getShowList: (state) => {
            return state.ouputimgList.slice(0, 4)
        },

    },
    actions: {
        init() {
            // if (Object.keys(this.txt2imgParams).length === 0) {
            //     console.log("初始化sdstore pinia仓库")
            //     this.txt2imgParams = JSON.parse(JSON.stringify(new txt2imgParams()))
            // }
            // if (Object.keys(this.img2imgParams).length === 0) {
            //     this.img2imgParams = JSON.parse(JSON.stringify(new img2imgParams()))
            // }
            // if (Object.keys(this.aimodelParams).length === 0) {
            //     this.aimodelParams = JSON.parse(JSON.stringify(new aimodelParams()))
            // }
            // if (Object.keys(this.aistudioParams).length === 0) {
            //     this.aistudioParams = JSON.parse(JSON.stringify(new aistudioParams()))
            // }
        },
        addImage(url: string) {
            const image = {
                url
            } as Image
            this.ouputimgList.unshift(image)
        },
        getSeed() {
            if (!this.isFixSeed) {
                this.txt2imgParams.seed = useTokenUtilgenerateRandomNumber(15)
            }
            return this.txt2imgParams.seed
        },
        deleteAll() {
            this.ouputimgList = []
        },
        deleteByIndex(index: any) {
            this.ouputimgList.splice(index, 1)
        },
        deleteByImageName(filename: string) {
            console.log(filename)
            this.ouputimgList.splice(this.ouputimgList.findIndex((item: OutputImage) => item.name === filename), 1)

        },
        addImageList(output: Image) {
            this.ouputimgList.unshift(output)

        },
        addOutputImage_Mask(filename: string, subfolder: string) {
            const image_mask = new image()
            if (subfolder) {
                image_mask.subfolder = subfolder

            } else {
                image_mask.subfolder = "output"
            }
            this.outputImage_Mask = image_mask

        },
    },
});
